package cn.tedu.mall.seckill.consemer;


import cn.tedu.mall.pojo.seckill.model.Success;
import cn.tedu.mall.seckill.config.RabbitMqComponentConfiguration;
import cn.tedu.mall.seckill.mapper.SeckillSkuMapper;
import cn.tedu.mall.seckill.mapper.SuccessMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@RabbitListener(queues = {RabbitMqComponentConfiguration.SECKILL_QUEUE})
public class SeckillQueueConsemer {
    @Autowired
    private SeckillSkuMapper seckillSkuMapper;
    @Autowired
    private SuccessMapper successMapper;
    @RabbitHandler
    public void process(Success success){
        //减少库存
        seckillSkuMapper.updateReduceStockBySkuId(success.getSkuId(),success.getQuantity());
        log.info("秒杀业务,库存减少成功");
        //新增Success对象到数据库
        successMapper.saveSuccess(success);
        log.info("Success新增成功");
        //编写异常处理方法,将消息大宋给死信队列,后续人工处理,是最后手段

    }
}
